﻿using daonaite.core.Attribute;
using daonaite.DAL.system;
using daonaite.Model.system.dept;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace daonaite.services.system
{
    [AutoLoad]
    public class deptService
    {
        private readonly D_dept deptcz;

        public deptService(D_dept deptcz)
        {
            this.deptcz = deptcz;
        }

        /// <summary>
        /// 查询树形部门
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<List<sys_dept>?> gettreedept(Dto_dept data)
        {
            return await deptcz.gettreedept(data);
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<int> add(sys_dept data)
        {
            if (data.fid == 0)
            {
                data.ancestors = "0";
            }
            else
            {
                var fdept = await deptcz.getAsync(data.fid);
                data.ancestors = $"{fdept.ancestors},{data.fid}";
            }

            return await deptcz.add(data);
        }

        /// <summary>
        /// 根据id获取部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<sys_dept> getAsync(int id)
        {
            return await deptcz.getAsync(id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<int> updateAsync(sys_dept data)
        {
            return await deptcz.updateAsync(data);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> delete(int id)
        {
            return await deptcz.delete(id);
        }
    }
}
